package com.ddm.pay;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

import com.ddm.pay.databinding.DialogFragmentDdmPayBinding;


public class DDMPayDialogFragment extends BaseDialogFragment<DialogFragmentDdmPayBinding> {
    private String loadUrl;
    //回调客户使用
    private GPH5PayShareListener gph5PayShareListener;
    //内部使用的
    private GPH5PayShareListener myGph5PayShareListener;
    private DDMWebViewJavascriptBridge dDMWebViewJavascriptBridge;
    //是否支付成功
    boolean isPaySuccess = false;
    //支付信息
    String mPayOrderMessage = "";

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_fragment_ddm_pay;
    }


    @Override
    public boolean isHeightMatchParent() {
        return true;
    }

    @Override
    void initView(Context context, View rootView, AttributeSet attrs) {
        mBinding.setView(this);
        mBinding.tvTitle.setText("支付");
        mBinding.webView.setTvTitle(mBinding.tvTitle);
        mBinding.webView.loadUrl(loadUrl);

    }

    @Override
    void initData(Context context) {


    }

    @Override
    void initEvent(Context context) {
        setDialogFragmentKeyListener(new DialogFragmentKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeDialogFragment();
                    return true;
                }
                return false;
            }
        });
        myGph5PayShareListener = new GPH5PayShareListener() {
            @Override
            public void onPurchaseCompleted(String payOrderMessage) {
                mPayOrderMessage = payOrderMessage;
                isPaySuccess = !TextUtils.isEmpty(payOrderMessage);
                gph5PayShareListener.onPurchaseCompleted(payOrderMessage);
            }

            @Override
            public void onPurchaseCancel() {
                dismiss();
                gph5PayShareListener.onPurchaseCancel();
            }
        };
        dDMWebViewJavascriptBridge = new DDMWebViewJavascriptBridge(getContext(), myGph5PayShareListener);
        mBinding.webView.addJavascriptInterface(dDMWebViewJavascriptBridge, "appObject");
    }

    DDMCancelPayDialogFragment ddmCancelPayDialogFragment;

    public void closeDialogFragment() {
        if (isPaySuccess) {
            dismiss();
        } else {
            if (ddmCancelPayDialogFragment != null && ddmCancelPayDialogFragment.isShowing()) {
                ddmCancelPayDialogFragment.dismiss();
                ddmCancelPayDialogFragment = null;
            }
            if (ddmCancelPayDialogFragment == null) {
                ddmCancelPayDialogFragment = new DDMCancelPayDialogFragment();
                ddmCancelPayDialogFragment.setGPH5PayShareListener(myGph5PayShareListener);
            }
            ddmCancelPayDialogFragment.show(getChildFragmentManager());
        }

    }

    public void initPayData(String url, GPH5PayShareListener gph5PayShareListener) {
        this.loadUrl = url;
        this.gph5PayShareListener = gph5PayShareListener;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dDMWebViewJavascriptBridge != null) {
            dDMWebViewJavascriptBridge.onDestroy();
        }
    }

}
